
/**
 * 各分子公司扣分统计图
 */
var avgGradeAcceptScopeBar = new CategoryBar("acceptScopeGradeCount");
var acceptScope;

$(function() {
	initQueryYear();
	$.ajaxSettings.async = false;
	getAvgGradeByAcceptScope();
	getAvgGradeByAcceptUnit(acceptScope);
	$.ajaxSettings.async = true;
});

function initQueryYear() {
	var date = new Date();
	var currentYear = date.getFullYear();
	$("#firstYear").val(currentYear-1);
	$('#firstYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
	$('#lastYear').val(currentYear);
	$('#lastYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}

/**
 * 以分子公司为单位，统计平均扣分
 * @returns
 */
function getAvgGradeByAcceptScope() {
	$.getJSON('/rest/grade/getAverageGradeGroupByAcceptScope', function(result) {
		initAvgGradeChart(result);
	});
}

function initAvgGradeChart(datas) {
	var xDatas = [];
	var grades = [];
	for(var i in datas) {
		xDatas.push(datas[i].dept);
		grades.push(datas[i].avgGrade);
	}
	avgGradeAcceptScopeBar.setxAxis(xDatas);
	avgGradeAcceptScopeBar.setyAxis(grades);
	var opt = avgGradeAcceptScopeBar.getOption();
	opt.legend.show=false;
	avgGradeAcceptScopeBar.setxAxisTitle("单位");
	avgGradeAcceptScopeBar.setyAxisTitle("扣分");
	avgGradeAcceptScopeBar.setyAxisName('扣分');
	avgGradeAcceptScopeBar.show();
	acceptScope = xDatas[0];
}

avgGradeAcceptScopeBar.getChart().on('click',function(params) {
	acceptScope = params.name;
	getAvgGradeByAcceptUnit(acceptScope);
});

/**
 * 统计指定分子公司下的各个供电局平均扣分情况
 * @returns
 */
function getAvgGradeByAcceptUnit(acceptScope) {
	if ($("#acceptUnitTitle").empty()) {
		$("#acceptUnitTitle").append(acceptScope + "各供电局平均扣分统计");
	}
	$.getJSON('/rest/grade/getAverageGradeGroupByAcceptUnit/' + acceptScope, function(result) {
		initAvgGradeUnitChart(result);
	});
}


function initAvgGradeUnitChart(datas) {
	/**
	 * 指定分子公司下的各供电局扣分统计图
	 */
	var avgGradeAcceptUnitBar = new CategoryBar("acceptUnitGradeCount");
	var xDatas = [];
	var grades = [];
	for(var i in datas) {
		xDatas.push(datas[i].dept);
		grades.push(datas[i].avgGrade);
	}
	avgGradeAcceptUnitBar.setxAxis(xDatas);
	avgGradeAcceptUnitBar.setyAxis(grades);
	var opt = avgGradeAcceptUnitBar.getOption();
	opt.legend.show=false;
	avgGradeAcceptUnitBar.setxAxisTitle("单位");
	avgGradeAcceptUnitBar.setyAxisTitle("扣分");
	avgGradeAcceptUnitBar.setyAxisName('扣分');
	avgGradeAcceptUnitBar.show();
}